//
//  NSMutableDictionary+category.m
//  FlowerTown
//
//  Created by lee on 2018/5/19.
//  Copyright © 2018年 HuaZhen. All rights reserved.
//

#import "NSMutableDictionary+category.h"

#import <objc/runtime.h>

@implementation NSMutableDictionary (category)
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method fromMethod = class_getInstanceMethod(objc_getClass("__NSDictionaryM"), @selector(setObject:forKey:));
        Method toMethod = class_getInstanceMethod(objc_getClass("__NSDictionaryM"), @selector(em_setObject:forKey:));
        method_exchangeImplementations(fromMethod, toMethod);
        
       
        method_exchangeImplementations(class_getInstanceMethod(NSClassFromString(@"__NSDictionaryM"), NSSelectorFromString(@"removeObjectForKey:")), class_getInstanceMethod(NSClassFromString(@"NSMutableDictionary"), NSSelectorFromString(@"st_removeObjectForKey:")));

    });
}

- (void)em_setObject:(id)emObject forKey:(NSString *)key {
    if (key == nil)
    {
        
        return;
    }
    else if (emObject == nil) {
        @try {
            [self em_setObject:@"" forKey:key];
        }
        @catch (NSException *exception) {
            NSLog(@"---------- %s Crash Because Method %s  ----------\n", class_getName(self.class), __func__);
            NSLog(@"%@", [exception callStackSymbols]);
//            emObject = [NSString stringWithFormat:@""];
//            [self em_setObject:emObject forKey:key];
        }
        @finally {}
    }else {
        [self em_setObject:emObject forKey:key];
    }
}

- (void)st_removeObjectForKey:(id)key {
    if (!key) {
        return;
    }
    [self st_removeObjectForKey:key];
}

@end
